<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class merk extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_merk');
        $this->load->model('m_kategoribarang');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        // load helpers
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/merk/list.html");

        //pencarian
        $search = $this->tsession->userdata('master_merk_search');
        $this->smarty->assign("search", $search);
        $merk_name = empty($search['merk_name']) ? '%' : '%' . $search['merk_name'] . '%';
        $kategoribarang_cd = empty($search['kategoribarang_cd']) ? '%' : $search['kategoribarang_cd'];

        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/merk/index/");
        $config['total_rows'] = $this->m_merk->get_count_all_merk_search(array($merk_name, $kategoribarang_cd));
        $config['uri_segment'] = 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($merk_name, $kategoribarang_cd, ($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_merk->get_all_merk_pagination_search($params));

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);


        // print_r($this->m_merk->get_all_merk_pagination($params));
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('master_merk_search');
        } else {
            $params = array(
                "merk_name" => $this->input->post("merk_name"),
                "kategoribarang_cd" => $this->input->post("kategoribarang_cd")
            );
            $this->tsession->set_userdata("master_merk_search", $params);
        }
        // redirect
        redirect("master/merk");
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/merk/add.html");

        //data
        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input 
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori Barang', 'trim|required');
        $this->tnotification->set_rules('merk_cd', 'Kode', 'trim|required|max_length[3]');
        $this->tnotification->set_rules('merk_name', 'Nama', 'trim|required|max_length[100]');

        //set parameter
        $params = array(
            $this->input->post('merk_cd'),
            $this->input->post('merk_name'),
            $this->input->post('kategoribarang_cd'),
            $this->com_user['user_id']
        );

        // cek merk   

        if ($this->m_merk->is_exist_merk(array($this->input->post('merk_cd'), $this->input->post('kategoribarang_cd')))) {
            $this->tnotification->sent_notification("error", 'Merk is not available');
        } else {
            // process
            if ($this->tnotification->run() !== FALSE) {
                // insert            
                if ($this->m_merk->insert($params)) {
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal disimpan");
                }
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        }

        // default redirect
        redirect("master/merk/add");
    }

    public function edit($kategoribarang_cd, $merk_cd) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/merk/edit.html");

        //data
        $result = $this->m_merk->get_merk_by_kategoribarang_cd_merk_cd(array($kategoribarang_cd, $merk_cd));
        $this->smarty->assign("result", $result);

        $rs_kategoribarang = $this->m_kategoribarang->get_all_kategoribarang();
        $this->smarty->assign("rs_kategoribarang", $rs_kategoribarang);

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        $this->tnotification->set_rules('merk_cd', 'Kode', 'trim|required|max_length[3]');
        $this->tnotification->set_rules('merk_name', 'Nama', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori ID', 'trim|required|max_length[100]');
        // process

        /*
          if ($this->m_merk->is_exist_merk(array($this->input->post('merk_cd'), $this->input->post('kategoribarang_cd')))) {
          $this->tnotification->sent_notification("error", 'Merk is not available');
          } else
         */

        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array(
                $this->input->post('merk_name'),
                $this->com_user['user_id'],
                $this->input->post('kategoribarang_cd'),
                $this->input->post('merk_cd')
            );
            // insert
            if ($this->m_merk->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/merk/edit/" . $this->input->post('kategoribarang_cd') . '/' . $this->input->post('merk_cd'));
    }

    public function delete($kategoribarang_cd, $merk_cd) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/merk/delete.html");
        //data
        $result = $this->m_merk->get_merk_by_kategoribarang_cd_merk_cd(array($kategoribarang_cd, $merk_cd));
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('merk_cd', 'Kode', 'trim|required|max_length[3]');
        $this->tnotification->set_rules('kategoribarang_cd', 'Kategori ID', 'trim|required|max_length[100]');
        
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('kategoribarang_cd'), $this->input->post('merk_cd')
            );
            // insert
            if ($this->m_merk->delete($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/merk/");
    }

}
